home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 15 / BBS in a box XV-2.iso / Files II / Prog / M / MacPerl 4.13 source.sit / Perl Source ƒ / MacPerl / MPScript.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-04  |  14.6 KB  |  836 lines  |  [TEXT/MPS ]

  1. /*********************************************************************
  2. Project    :    MacPerl            -    Real Perl Application
  3. File        :    MPScript.c        -    Handle scripts
  4. Author    :    Matthias Neeracher
  5. Language    :    MPW C
  6.  
  7. $Log: MPScript.c,v $
  8. Revision 1.2  1994/05/04  02:54:19  neeri
  9. Always keep the right resource file in front.
  10.  
  11. Revision 1.1  1994/02/27  23:01:56  neeri
  12. Initial revision
  13.  
  14. Revision 0.2  1993/10/14  00:00:00  neeri
  15. Run front window
  16.  
  17. Revision 0.1  1993/08/17  00:00:00  neeri
  18. Set up correct default directory
  19.  
  20. *********************************************************************/
  21.  
  22. #include <AERegistry.h>
  23. #include <String.h>
  24. #include <TFileSpec.h>
  25. #include <setjmp.h>
  26. #include <sys/types.h>
  27. #include <ctype.h>
  28. #include <stdio.h>
  29. #include <fcntl.h>
  30. #include <unistd.h>
  31. #include <Signal.h>
  32. #include <StandardFile.h>
  33. #include <Resources.h>
  34. #include <PLStringFuncs.h>
  35. #include <SysEqu.h>
  36.  
  37. #include "MPScript.h"
  38. #include "MPWindow.h"
  39. #include "MPAppleEvents.h"
  40.  
  41. int                 run_perl(int, char **, char **);
  42. void                 reenter();
  43. extern Handle    PerlReply;
  44. extern int        PerlQuit;
  45. extern char     gPseudoFileName[];
  46.  
  47. #ifndef RUNTIME
  48. pascal Boolean GetScriptFilter(ParmBlkPtr    info, void * data)
  49. {
  50. #pragma unused(data)
  51. #else
  52. pascal Boolean GetScriptFilter(ParmBlkPtr    info)
  53. {
  54. #endif
  55.     switch (info->fileParam.ioFlFndrInfo.fdType) {
  56.     case 'APPL':
  57.         switch (info->fileParam.ioFlFndrInfo.fdCreator) {
  58.         case MPRtSig:
  59.             return false;
  60.         case MPAppSig:
  61.             return !info->fileParam.ioFlLgLen;
  62.         default:
  63.             return true;
  64.         }
  65.     case 'TEXT':
  66.         return false;
  67.     default:
  68.         return true;
  69.     }
  70. }
  71.  
  72. #ifndef RUNTIME
  73.  
  74. #define gsDebugItem        10
  75.  
  76. pascal short GetScriptHook(short item, DialogPtr dlg, void * params)
  77. {
  78.     short                kind;
  79.     ControlHandle    dbg;
  80.     Rect                r;
  81.     Boolean *        par = (Boolean *) params;
  82.     
  83.     if (GetWRefCon(dlg) != 'stdf')
  84.         return item;
  85.  
  86.     switch (item) {
  87.     case sfHookFirstCall:
  88.         *par    =    false;
  89.     
  90.         return sfHookFirstCall;
  91.     case gsDebugItem:
  92.         *par = !*par;
  93.         
  94.         GetDItem(dlg, item, &kind, (Handle *) &dbg, &r);
  95.         
  96.         SetCtlValue(dbg, *par);
  97.         
  98.         return sfHookNullEvent;
  99.     default:
  100.         return item;
  101.     }
  102. }
  103.  
  104. static void SendScriptEvent(
  105.     DescType argType, 
  106.     Ptr         argPtr, 
  107.     Handle    argHdl,
  108.     Size         argSize, 
  109.     Boolean     debug)
  110. {
  111.     OSErr                    err;
  112.     AppleEvent            cmd, repl;
  113.     AEAddressDesc        addr;
  114.     
  115.     if (err = MakeSelfAddress(&addr))
  116.         goto failedAddress;
  117.         
  118.     if (err = 
  119.         AECreateAppleEvent(
  120.             kAEMiscStandards, kAEDoScript, &addr, 
  121.             kAutoGenerateReturnID, kAnyTransactionID, 
  122.             &cmd)
  123.     )
  124.         goto failedAppleEvent;
  125.     
  126.     if (argHdl) {
  127.         HLock(argHdl);
  128.         argPtr = *argHdl;
  129.     }
  130.     
  131.     if (err = AEPutParamPtr(&cmd, keyDirectObject, argType, argPtr, argSize))
  132.         goto failedParam;
  133.     
  134.     if (debug)
  135.         if (err =
  136.             AEPutParamPtr(
  137.                 &cmd, 'DEBG', 
  138.                 typeBoolean, (Ptr) &debug, sizeof(Boolean))
  139.         )
  140.             goto failedParam;
  141.         
  142.     err =
  143.         AESend(
  144.             &cmd,
  145.             &repl,
  146.             kAENoReply+kAEAlwaysInteract,
  147.             kAENormalPriority,
  148.             kAEDefaultTimeout,
  149.             nil,
  150.             nil);
  151.  
  152.     AEDisposeDesc(&repl);
  153. failedParam:    
  154.     if (argHdl)
  155.         HUnlock(argHdl);
  156.         
  157.     AEDisposeDesc(&cmd);
  158. failedAppleEvent:
  159.     AEDisposeDesc(&addr);
  160. failedAddress:
  161.     ;
  162. }
  163.  
  164. static SFTypeList    PerlFileTypes    = {'TEXT', 'APPL'};
  165.  
  166. pascal void DoScriptMenu(short theItem)
  167. {
  168.     StandardFileReply    reply;
  169.     Point                    where;
  170.     Boolean                debug;
  171.     
  172.     where.h = where.v = -1;
  173.     
  174.     switch (theItem) {
  175.     case pmRun:
  176.         CustomGetFile(
  177.             GetScriptFilter,
  178.             2,
  179.             PerlFileTypes,
  180.             &reply,
  181.             GetScriptDialog,
  182.             where,
  183.             GetScriptHook,
  184.             (ModalFilterYDProcPtr) nil,
  185.             nil,
  186.             (ActivateYDProcPtr) nil,
  187.             &debug);
  188.         if (reply.sfGood)
  189.             SendScriptEvent(typeFSS, (Ptr) &reply.sfFile, nil, sizeof(FSSpec), debug);
  190.         break;
  191.     case pmRunFront:
  192.         {
  193.             DPtr    doc = DPtrFromWindowPtr(FrontWindow());
  194.             
  195.             if (!doc || doc->kind != kDocumentWindow)
  196.                 break;
  197.             
  198.             if (doc->dirty || !doc->u.reg.everSaved) {
  199.                 if (doc->u.reg.everSaved)
  200.                     strcpy(gPseudoFileName, FSp2FullPath(&doc->theFSSpec));
  201.                 else
  202.                     getwtitle(FrontWindow(), gPseudoFileName);
  203.  
  204.                 SendScriptEvent(
  205.                     typeChar, nil, (*doc->theText)->hText, 
  206.                     GetHandleSize((*doc->theText)->hText),
  207.                     false);
  208.             } else
  209.                 SendScriptEvent(typeFSS, (Ptr) &doc->theFSSpec, nil, sizeof(FSSpec), false);
  210.         }
  211.         break;
  212.     }
  213. }
  214.  
  215. #endif
  216.  
  217. static char *        PerlArgs[]     = {
  218.     "MacPerl",
  219.     0,
  220.     0,
  221.     0,
  222.     0,
  223.     0,
  224.     0,
  225.     0,
  226.     0,
  227.     0,
  228.     0,
  229.     0,
  230.     0,
  231.     0,
  232.     0,
  233.     0,
  234.     0,
  235.     0,
  236.     0,
  237.     0,
  238.     0,
  239.     0,
  240.     0,
  241.     0,
  242.     0,
  243.     0,
  244.     0,
  245.     0
  246. };
  247.  
  248. static char *        PerlEnviron[] = {
  249.     "PERLDB=require \"macperldb.pl\"",
  250.     0,
  251.     0
  252. };
  253.  
  254. extern char * perldbgname;
  255.  
  256. pascal void InitPerlEnviron()
  257. {
  258.     char **     env = PerlEnviron;
  259.     char *    eq;
  260.     
  261.     while (*env)
  262.         if (eq = strchr(*env++, '='))
  263.             *eq = 0;
  264.     
  265.     perldbgname = "Dev:Console:Debug Log";
  266. }
  267.  
  268. char * getenv(char * var)
  269. {
  270.     char ** env;
  271.     
  272.     for (env = PerlEnviron; *env; ++env)
  273.         if (!strcmp(*env, var))
  274.             return *env + strlen(*env) + 1;
  275.     
  276.     return nil;
  277. }
  278.  
  279. static jmp_buf    ExitPerl;
  280.  
  281. void real_exit(int status);
  282.  
  283. void exit(int status)
  284. {
  285.     if (gRunningPerl)
  286.         longjmp(ExitPerl, -status);
  287.     else
  288.         real_exit(status);
  289. }
  290.  
  291. typedef void (*atexitfn)();
  292.  
  293. static atexitfn     PerlExitFn[20];
  294. static int            PerlExitCnt;
  295.  
  296. int real_atexit(atexitfn func);
  297.  
  298. int atexit(atexitfn func)
  299. {
  300.     if (gRunningPerl)
  301.         PerlExitFn[PerlExitCnt++] = func;
  302.     else
  303.         return real_atexit(func);
  304.         
  305.     return 0;
  306. }
  307.  
  308. void CleanupPerl()
  309. {
  310.     int i;
  311.     extern FILE * _lastbuf;
  312.  
  313.     UseResFile(gAppFile);
  314.  
  315.     // Borrowed from GUSI
  316.     
  317.     // Close stdio files (necessary to flush buffers)
  318.     // This implementation is not nice, but who cares ?
  319.     // In case you wonder, _iob is defined in <stdio.h>
  320.  
  321.     for (i = 0; _iob+i<_lastbuf; i++)
  322.         fflush(_iob+i);
  323.  
  324.     for (i = 0; _iob+i<_lastbuf; i++)
  325.         fclose(_iob+i);
  326.  
  327.     // Close all files
  328.  
  329.     for (i = 0; i<FD_SETSIZE; ++i)
  330.         close(i);
  331.  
  332.     while (PerlExitCnt)
  333.         PerlExitFn[--PerlExitCnt]();
  334.  
  335.     UseResFile(gAppFile);
  336.     reenter();
  337.     
  338.     open("Dev:Console", O_RDONLY);
  339.     open("Dev:Console", O_WRONLY);
  340.     open("Dev:Console", O_WRONLY);
  341.     
  342.     fopen("Dev:Console", "r");
  343.     fopen("Dev:Console", "w");
  344.     fopen("Dev:Console", "w");
  345. }
  346.  
  347. enum {
  348.     extractDone            = -4,
  349.     extractDir            = -3,
  350.     extractCpp            = -2,
  351.     extractDebug         = -1
  352. };
  353.  
  354. typedef char * (*ArgExtractor)(void * data, int index);
  355.  
  356. pascal void RunScript(ArgExtractor extractor, void * data)
  357. {
  358.     int        ArgC;
  359.     short        resFile;
  360.     Handle    libs;
  361.     Str255    lib;
  362.     char    *    res;
  363.     int        i;
  364.  
  365.     PtrToHand("PERLLIB", &libs, 8);
  366.     
  367.     resFile = CurResFile();
  368.     UseResFile(gPrefsFile);
  369.     
  370.     for (ArgC = 1; ; ++ArgC) {
  371.         GetIndString(lib, LibraryPaths, ArgC);
  372.         
  373.         if (!lib[0])
  374.             break;
  375.             
  376.         if (ArgC > 1)
  377.             PtrAndHand(",", libs, 1);
  378.         
  379.         PtrAndHand(lib+1, libs, lib[0]);
  380.     }
  381.     
  382.     UseResFile(resFile);
  383.     
  384.     if (PerlEnviron[1])
  385.         DisposePtr(PerlEnviron[1]);
  386.     
  387.     PerlEnviron[1] = NewPtr(GetHandleSize(libs)+1);
  388.     BlockMove(*libs, PerlEnviron[1], GetHandleSize(libs));
  389.     PerlEnviron[1][GetHandleSize(libs)] = 0;
  390.     DisposeHandle(libs);
  391.     
  392.     ArgC = 1;
  393.     
  394.     {
  395.         char        path[256];
  396.     
  397.         strcpy(path, extractor(data, extractDir));
  398.         chdir(path);
  399.     }
  400.     
  401.     if ((res = extractor(data, extractDebug)) && *res == 'y')
  402.         PerlArgs[ArgC++] = "-d";
  403.  
  404.     if ((res = extractor(data, extractCpp)) && *res == 'y')
  405.         PerlArgs[ArgC++] = "-P";
  406.  
  407.     if (res = extractor(data, 1)) {
  408.         if (gPerlPrefs.checkType && !gPseudoFile) 
  409.             PerlArgs[ArgC++] = "-x";
  410.         
  411.         PerlArgs[ArgC++] = res;
  412.     
  413.         for (i=2; PerlArgs[ArgC] = extractor(data, i); ++i, ++ArgC);
  414.     }
  415.     
  416.     extractor(data, extractDone);
  417.     
  418.     UseResFile(gAppFile);
  419.     
  420.     gRunningPerl     =  true;
  421.     PerlQuit            =    0;
  422.     ShowWindowStatus();
  423.     
  424.     signal(SIGINT, exit);
  425.     
  426.     if (!setjmp(ExitPerl))             
  427.         run_perl(ArgC, PerlArgs, PerlEnviron);
  428.     
  429.     CleanupPerl();
  430.     gRunningPerl = false;
  431.     
  432.     if (gScriptFile != gAppFile) {
  433.         CloseResFile(gScriptFile);
  434.         
  435.         gScriptFile = gAppFile;
  436.     }
  437.     
  438.     ShowWindowStatus();
  439.  
  440.     for (i=1; PerlArgs[i]; ++i)
  441.         DisposPtr(PerlArgs[i]);
  442.  
  443.     switch (PerlQuit) {
  444.     case 2:
  445. #ifdef RUNTIME
  446.     case 1:
  447. #endif
  448.         gQuitting = true;
  449.     }
  450. }
  451.  
  452. char * AEExtractor(void * data, int index)
  453. {
  454.     DescType            type;
  455.     Size                size;
  456.     Boolean            arg;
  457.     AppleEvent *     event;
  458.     FSSpec            spec;
  459.     AEKeyword        keywd;
  460.     static AEDesc    params = {'????', nil};
  461.     char *            retarg;
  462.     char *            path;
  463.     
  464.     event = (AppleEvent *) data;
  465.     
  466.     switch (index) {
  467.     case extractDone:
  468.         gRuntimeScript = nil;
  469.         
  470.         if (params.dataHandle)
  471.             AEDisposeDesc(¶ms);
  472.         
  473.         return nil;
  474.     case extractDir:
  475.         if (gRuntimeScript 
  476.         || (!params.dataHandle 
  477.             && AEGetParamDesc(event, keyDirectObject, typeAEList, ¶ms))
  478.         || AEGetNthPtr(
  479.                 ¶ms, 1, typeFSS, &keywd, &type, 
  480.                 (Ptr) &spec, sizeof(FSSpec), &size)
  481.         ) {
  482.             spec.vRefNum    =    gAppVol;
  483.             spec.parID        =    gAppDir;
  484.         } else {
  485.             short    res    = CurResFile();
  486.             
  487.             gScriptFile = HOpenResFile(spec.vRefNum, spec.parID, spec.name, fsRdPerm);
  488.             
  489.             if (gPseudoFile    =     Get1NamedResource('TEXT', "\p!")) {
  490.                 strcpy(gPseudoFileName, FSp2FullPath(&spec));
  491.                 
  492.                 DetachResource(gPseudoFile);
  493.             }
  494.  
  495.             UseResFile(res);
  496.         } 
  497.         
  498.         FSpUp(&spec);
  499.         
  500.         return FSp2FullPath(&spec);
  501.     case extractDebug:
  502.         if (AEGetParamPtr(event, 'DEBG', typeBoolean, &type, (Ptr) &arg, 1, &size))
  503.             return nil;
  504.         else
  505.             return arg ? "y" : "n";
  506.     case extractCpp:
  507.         if (AEGetParamPtr(event, 'PREP', typeBoolean, &type, (Ptr) &arg, 1, &size))
  508.             return nil;
  509.         else
  510.             return arg ? "y" : "n";
  511.     default:
  512.         if (gRuntimeScript)
  513.             --index;
  514.         else if (index == 1 && gPseudoFile)
  515.             return "Dev:Pseudo";
  516.             
  517.         if (!index) {
  518.             gPseudoFile = gRuntimeScript;
  519.             
  520.             return "Dev:Pseudo";
  521.         }
  522.         
  523.         if (!params.dataHandle)
  524.             if (AEGetParamDesc(event, keyDirectObject, typeAEList, ¶ms))
  525.                 return nil;
  526.  
  527.         if (AEGetNthPtr(
  528.                 ¶ms, index, typeFSS,
  529.                 &keywd, &type, 
  530.                 (Ptr) &spec, sizeof(FSSpec), &size)
  531.         )     if (index == 1 && !gRuntimeScript) {
  532.                 AEDesc    script;
  533.                     
  534.                 if (AEGetNthDesc(¶ms, index, typeChar, &keywd, &script))
  535.                     return nil;
  536.                 
  537.                 gPseudoFile = script.dataHandle;
  538.                 
  539.                 if (!gPseudoFileName[0])
  540.                     strcpy(gPseudoFileName, "<AppleEvent>");
  541.                 
  542.                 return "Dev:Pseudo";
  543.             } else if (AEGetNthPtr(
  544.                 ¶ms, index, typeChar,
  545.                 &keywd, &type, 
  546.                 nil, 0, &size)
  547.             )
  548.                 return nil;
  549.             else {
  550.                 retarg = NewPtr(size+1);
  551.                 retarg[size] = 0;
  552.                 
  553.                 if (AEGetNthPtr(
  554.                     ¶ms, index, typeChar,
  555.                     &keywd, &type, 
  556.                     retarg, size, &size)
  557.                 ) {
  558.                     DisposPtr(retarg);
  559.                     
  560.                     return nil;
  561.                 } else
  562.                     return retarg;
  563.             }
  564.                 
  565.         path = FSp2FullPath(&spec);
  566.         retarg = NewPtr(strlen(path)+1);
  567.             
  568.         strcpy(retarg, path);
  569.             
  570.         return retarg;
  571.     }            
  572. }
  573.  
  574. char * StupidExtractor(void * data, int index)
  575. {
  576.     FSSpec    *        spec;
  577.     FSSpec            dir;
  578.     char *            retarg;
  579.     char *            path;
  580.     
  581.     spec = (FSSpec *) data;
  582.     
  583.     switch (index) {
  584.     case extractDone:
  585.     case extractDebug:
  586.     case extractCpp:
  587.         return nil;
  588.     case extractDir:
  589.         dir = *spec;
  590.         
  591.         {
  592.             short    res    = CurResFile();
  593.             
  594.             gScriptFile = HOpenResFile(dir.vRefNum, dir.parID, dir.name, fsRdPerm);
  595.             
  596.             if (gPseudoFile    =     Get1NamedResource('TEXT', "\p!")) {
  597.                 strcpy(gPseudoFileName, FSp2FullPath(spec));
  598.                 
  599.                 DetachResource(gPseudoFile);
  600.             }
  601.             
  602.             UseResFile(res);
  603.         } 
  604.         
  605.         FSpUp(&dir);
  606.         
  607.         return FSp2FullPath(&dir);
  608.     default:
  609.         if (index > 1)
  610.             return nil;
  611.  
  612.         if (gPseudoFile)
  613.             return "Dev:Pseudo";
  614.             
  615.         path = FSp2FullPath(spec);
  616.         retarg = NewPtr(strlen(path)+1);
  617.             
  618.         strcpy(retarg, path);
  619.             
  620.         return retarg;
  621.     }            
  622. }
  623.  
  624. char * YeOldeExtractor(void * data, int index)
  625. {
  626.     long        count;
  627.     char *    retarg;
  628.     char *    path;
  629.     FSSpec    spec;
  630.     AppFile    arg;
  631.     
  632.     count = (long) data;
  633.     
  634.     switch (index) {
  635.     case extractDone:
  636.         gRuntimeScript = nil;
  637.     case extractDebug:
  638.     case extractCpp:
  639.         return nil;
  640.     case extractDir:
  641.         if (gRuntimeScript) {
  642.             spec.vRefNum = gAppVol;
  643.             spec.parID   = gAppDir;
  644.         } else {
  645.             short    res    =    CurResFile();
  646.             
  647.             GetAppFiles(1, &arg);
  648.     
  649.             WD2FSSpec(arg.vRefNum, arg.fName, &spec);
  650.             
  651.             gScriptFile    =    HOpenResFile(spec.vRefNum, spec.parID, spec.name, fsRdPerm);
  652.             
  653.             if (gPseudoFile    =     Get1NamedResource('TEXT', "\p!")) {
  654.                 strcpy(gPseudoFileName, FSp2FullPath(&spec));
  655.                 
  656.                 DetachResource(gPseudoFile);
  657.             }
  658.             
  659.             UseResFile(res);
  660.         }
  661.         
  662.         FSpUp(&spec);
  663.         
  664.         return FSp2FullPath(&spec);
  665.     default:
  666.         if (index - (gRuntimeScript != 0) > count)
  667.             return nil;
  668.  
  669.         if (gRuntimeScript)
  670.             --index;
  671.         else if (index == 1 && gPseudoFile)
  672.             return "Dev:Pseudo";
  673.             
  674.         if (!index) {
  675.             gPseudoFile = gRuntimeScript;
  676.             
  677.             return "Dev:Pseudo";
  678.         }
  679.  
  680.         GetAppFiles(index, &arg);
  681.     
  682.         WD2FSSpec(arg.vRefNum, arg.fName, &spec);
  683.         
  684.         path = FSp2FullPath(&spec);
  685.         retarg = NewPtr(strlen(path)+1);
  686.             
  687.         strcpy(retarg, path);
  688.             
  689.         return retarg;
  690.     }            
  691. }
  692.  
  693. pascal OSErr DoScript(const AppleEvent *event, AppleEvent *reply, long refCon)
  694. {
  695. #pragma unused (refCon)
  696.  
  697.     if (gRunningPerl) {
  698.         const AppleEvent * e[2];
  699.         
  700.         e[0] = event;
  701.         e[1] = reply;
  702.         
  703.         PtrAndHand((Ptr) e, (Handle) gWaitingScripts, 8);
  704.         
  705.         return AESuspendTheCurrentEvent(event);
  706.     }
  707.     
  708.     RunScript(AEExtractor, event);
  709.     
  710.     if (PerlReply) {
  711.         HLock(PerlReply);
  712.         AEPutParamPtr(
  713.                     reply, keyDirectObject,
  714.                     typeChar, *PerlReply, GetHandleSize(PerlReply));
  715.         DisposeHandle(PerlReply);
  716.         PerlReply = nil;
  717.     }
  718.     
  719.     return noErr;
  720. }
  721.  
  722. #ifdef RUNTIME
  723.  
  724. pascal void DoScriptMenu(short theItem)
  725. {
  726.     switch (theItem) {
  727.     case pmRun:
  728.         {
  729.             Point         wh;
  730.             SFTypeList    types;
  731.             SFReply        reply;
  732.             FSSpec        spec;
  733.          
  734.             wh.h = wh.v = 75;
  735.             types[0]    = 'TEXT';
  736.             types[1]    = 'APPL';
  737.          
  738.             SFGetFile(wh, "", GetScriptFilter, 2, types, (DlgHookProcPtr) nil, &reply);
  739.         
  740.             if (reply.good) {
  741.                 WD2FSSpec(reply.vRefNum, reply.fName, &spec);
  742.                         
  743.                 RunScript(StupidExtractor, &spec);
  744.             }
  745.         }
  746.         break;
  747.     case pmRunFront:
  748.         {
  749.             DPtr    doc = DPtrFromWindowPtr(FrontWindow());
  750.             
  751.             if (!doc || doc->kind != kDocumentWindow)
  752.                 break;
  753.             
  754.             if (doc->dirty || !doc->u.reg.everSaved) {
  755.                 gRuntimeScript = (*doc->theText)->hText;
  756.                 
  757.                 HandToHand(&gRuntimeScript);
  758.                 
  759.                 if (doc->u.reg.everSaved)
  760.                     strcpy(gPseudoFileName, FSp2FullPath(&doc->theFSSpec));
  761.                 else
  762.                     getwtitle(FrontWindow(), gPseudoFileName);
  763.                 
  764.                 RunScript(YeOldeExtractor, (void *) 0);
  765.             } else
  766.                 RunScript(StupidExtractor, &doc->theFSSpec);
  767.         }
  768.         break;
  769.     }
  770. }
  771. #endif
  772.  
  773. pascal Boolean DoRuntime()
  774. {
  775.     short        message;
  776.     short        count;
  777.     FSSpec    spec;
  778.     
  779.     if (gRuntimeScript = Get1NamedResource('TEXT', "\p!")) {
  780.         spec.vRefNum     =     gAppVol;
  781.         spec.parID        =    gAppDir;
  782.         PLstrcpy(spec.name, (StringPtr) CurApName);
  783.         strcpy(gPseudoFileName, FSp2FullPath(&spec));
  784.         
  785.         DetachResource(gRuntimeScript);
  786.     }
  787.  
  788. #ifndef RUNTIME
  789.     return false;
  790. #else
  791.     if (gAppleEventsImplemented)
  792.         return false;
  793.         
  794.     CountAppFiles(&message, &count);
  795.     
  796.     if (count) {
  797.         if (message == appPrint) {
  798.             int        i;
  799.            AppFile    arg;
  800.         
  801.             for (i=0; i++<count; ) {
  802.                 GetAppFiles(i, &arg);
  803.             
  804.                 WD2FSSpec(arg.vRefNum, arg.fName, &spec);
  805.                 
  806.                 if (!IssueAEOpenDoc(spec)) {
  807.                     IssuePrintWindow(FrontWindow());
  808.                     IssueCloseCommand(FrontWindow());
  809.                 }
  810.             }
  811.             
  812.             return true;
  813.         } 
  814.     } else {
  815.         if (!gRuntimeScript) {
  816.             int        i;
  817.            AppFile    arg;
  818.         
  819.             for (i=0; i++<count; ) {
  820.                 GetAppFiles(i, &arg);
  821.             
  822.                 WD2FSSpec(arg.vRefNum, arg.fName, &spec);
  823.                 
  824.                 IssueAEOpenDoc(spec);
  825.             }
  826.             
  827.             return false;
  828.         }
  829.     }
  830.     
  831.     RunScript(YeOldeExtractor, (void *) count);
  832.     
  833.     return gQuitting;
  834. #endif
  835. }
  836.